global class DataSourceUtil{
Webservice static String purgeUsers(String str){
User[] users =null;
CallidusDataSource__c[] callidusUsers = null;
Integer usersize =0;
Integer i =0;
System.LoggingLevel level =
LoggingLevel.DEBUG;

callidusUsers = [Select Id,Name1__c, Name2__c, Name3__c, Name4__c, Name5__c, Name6__c, Name7__c, Name8__c, Name9__c, Name10__c, Name11__c, Name12__c, Name13__c, Name14__c, Name15__c, Name16__c, Name17__c, Name18__c, Name19__c, Name20__c, Name21__c, Name22__c, Name23__c, Name24__c, Name25__c, Name26__c, Name27__c, Name28__c, Name29__c, Name30__c, Value1__c, Value2__c, Value3__c, Value4__c, Value5__c, Value6__c, Value7__c, Value8__c, Value9__c, Value10__c, Value11__c, Value12__c, Value13__c, Value14__c, Value15__c, Value16__c, Value17__c, Value18__c, Value19__c, Value20__c, Value21__c, Value22__c, Value23__c, Value24__c, Value25__c, Value26__c, Value27__c, Value28__c, Value29__c, Value30__c from CallidusDataSource__c];

for(CallidusDataSource__c user:callidusUsers){
//user.Id = user.Id;
user.Name1__c='';
user.Name2__c='';
user.Name3__c='';
user.Name4__c='';
user.Name5__c='';
user.Name6__c='';
user.Name7__c='';
user.Name8__c='';
user.Name9__c='';
user.Name10__c='';
user.Name11__c='';
user.Name12__c='';
user.Name13__c='';
user.Name14__c='';
user.Name15__c='';
user.Name16__c='';
user.Name17__c='';
user.Name18__c='';
user.Name19__c='';
user.Name20__c='';
user.Name21__c='';
user.Name22__c='';
user.Name23__c='';
user.Name24__c='';
user.Name25__c='';
user.Name26__c='';
user.Name27__c='';
user.Name28__c='';
user.Name29__c='';
user.Name30__c='';

user.Value1__c='';
user.Value2__c='';
user.Value3__c='';
user.Value4__c='';
user.Value5__c='';
user.Value6__c='';
user.Value7__c='';
user.Value8__c='';
user.Value9__c='';
user.Value10__c='';
user.Value11__c='';
user.Value12__c='';
user.Value13__c='';
user.Value14__c='';
user.Value15__c='';
user.Value16__c='';
user.Value17__c='';
user.Value18__c='';
user.Value19__c='';
user.Value20__c='';
user.Value21__c='';
user.Value22__c='';
user.Value23__c='';
user.Value24__c='';
user.Value25__c='';
user.Value26__c='';
user.Value27__c='';
user.Value28__c='';
user.Value29__c='';
user.Value30__c='';
i++;

}

List<CallidusDataSource__c> callidusList=callidusUsers;
try{
update callidusList;
}catch(DmlException dme){

 return ''+usersize+dme;
 }// 'catch'
 callidusList.clear();
 return 'success';
}
  
  

Webservice static String generateUsers(String str){

User[] users =null;
CallidusDataSource__c[] callidusUsers = null;
CallidusDataSource__c[] insertRecords = null;
Integer usersize =0;
Integer i =0;
if(str == 'fromTestMethod')
{
    users = [SELECT Id, LastName, FirstName,Title, UserRole.Id, UserRole.Name FROM User order by FirstName desc limit 20];
    CallidusDataSource__c[] cds =  [SELECT Id, Name, LastName__c, FirstName__c, UserID__c, UserRoleId__c, UserRoleName__c,User_Title__c from CallidusDataSource__c order by FirstName__c desc limit 200];
    List<CallidusDataSource__c> callidusList = new List<CallidusDataSource__c>();
    for(User us: users)
    {
        for(CallidusDataSource__c cd : cds)
        {
            if(us.Id == cd.UserID__c)
            {
                callidusList.add(cd);
                break;
            }
                
        }
    }
    delete callidusList;
    callidusUsers = [SELECT Id, Name, LastName__c, FirstName__c, UserID__c, UserRoleId__c, UserRoleName__c,User_Title__c from CallidusDataSource__c order by FirstName__c desc limit 10];
}
else
{
    users = [SELECT Id, LastName, FirstName, IsActive, UserRole.Id, UserRole.Name,Title FROM User u where u.FirstName != 'License' and u.IsActive = true order by FirstName];
    callidusUsers = [SELECT Id, Name, LastName__c, FirstName__c, UserID__c, UserRoleId__c, UserRoleName__c,User_Title__c from CallidusDataSource__c order by FirstName__c];
}

if(users.size() > callidusUsers.size() && users.size() != callidusUsers.size())
{
    insertRecords = new CallidusDataSource__c[users.size() - callidusUsers.size()];
}

Integer m = 0 ;
String flag = 'false'; 

for(User user:users){
    flag = 'false';
    for(CallidusDataSource__c cUser: callidusUsers)
    {
        if(user.Id == cUser.UserID__c)
        {
            if(user.FirstName != null)
            {
                cUser.Name = user.FirstName+' '+user.LastName;
            }
            else if(user.FirstName == null)
            {
                cUser.Name = user.LastName;
            }
        
            cUser.FirstName__c=user.FirstName;
            cUser.LastName__c=user.LastName;
            cUser.UserID__c=user.Id;
            cUser.UserRoleId__c=user.UserRole.Id;
            cUser.UserRoleName__c=user.UserRole.Name;
            cUser.User_Title__c=user.Title;

            flag = 'true';
            break;
        }
    }
    if(insertRecords!= null && insertRecords.size() > 0)
    {
        if(flag == 'false' && m != insertRecords.size())
        {
            insertRecords[m] = new CallidusDataSource__c();
            if(user.FirstName != null)
            {
                insertRecords[m].Name = user.FirstName+' '+user.LastName;
            }
            else if(user.FirstName == null)
            {
                insertRecords[m].Name = user.LastName;
            }

            insertRecords[m].FirstName__c=user.FirstName;
            insertRecords[m].LastName__c=user.LastName;
            insertRecords[m].UserID__c=user.Id;
            insertRecords[m].UserRoleId__c=user.UserRole.Id;
            insertRecords[m].UserRoleName__c=user.UserRole.Name;
            insertRecords[m].User_Title__c=user.Title;
            m++;
        }
    }
}//end of for(User user.........
//return callidusUsers.size()+'';

    List<CallidusDataSource__c> callidusList= new List<CallidusDataSource__c>();
    callidusList = callidusUsers;
    try{
        upsert callidusList Id;
    }
    catch(Exception dme)
    {   
        return ''+dme;
    }// catch'

if(insertRecords!= null && insertRecords.size() > 0)
{
    callidusList.clear();
    for(Integer y=0; y<insertRecords.size(); y++)
    {
        if(insertRecords[y] != null)
        {
            callidusList.add(insertRecords[y]);
                    
        }
    }
    if(callidusList.size() > 0)
    {
        try{
         insert callidusList;
        }
        catch(DmlException dme)
        {   
            return ''+dme;
        }// 'catch'
    }
}
    return 'success';
}

static Boolean testFlag = false;
    Webservice static String populateBatchedUsersForSelectDataSource(String dataSourceListArray, String templateId)
    {
        String[] userIdList = new List<String>();
        String[] roleIdList = new List<String>();
        String batchId = '';
        if(testFlag == false)
            batchId = PopulateBatchedUsers.populateObject(userIdList, roleIdList, templateId, 'null');
        
        BatchedUser__c bu;    
        String[] dataSourceList = dataSourceListArray.split(',');
        for(Integer i=0; i <dataSourceList.size(); i++)
        {
            CallidusDataSource__c[] cds  = [select UserID__c, User_Title__c, UserRoleId__c from CallidusDataSource__c where Id = :dataSourceList[i]];
            for(CallidusDataSource__c c: cds)
            {
              bu = new BatchedUser__c();
              bu.TemplateId__c = templateId;
              bu.UserId__c = c.UserID__c;
              bu.UserRoleId__c = c.UserRoleId__c;
              bu.Batchid__c = batchId;
              bu.User_Title__c = c.User_Title__c;
              try
              {
                  insert bu;
              }
              catch(Exception e)
              {
                  return e.getMessage();
              }
            }
        }

        return batchId;
    }

    Webservice static String[] getTemplateDetails()
    {
        String[] tempRec = new String[2];
        Template__c[] t = [select Id, Name from Template__c where Status__c = 'In Use' order by CreatedDate desc limit 1];
        if(t.size() > 0)
        {
            tempRec[0] = t[0].Id;
            tempRec[1] = t[0].Name;
            return tempRec;
        }
        return null;
    }

    static testMethod void testCase() 
    {
        purgeUsers('test');
        System.assertEquals('success', generateUsers('fromTestMethod'));
        String[] stArr = getTemplateDetails();
        testFlag = true;
        populateBatchedUsersForSelectDataSource('a0G400000004XpI, a0G400000004XpH', 'a0D400000020eAQ');
        System.assertEquals('success', generateUsers(''));

    }
  
 }